package com.tjtt.tdm.system.action;

import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.tjtt.common.utils.StringUtil;
import com.tjtt.tdm.base.BaseAction;
import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.base.PubMethod;
import com.tjtt.tdm.exception.ServiceException;
import com.tjtt.tdm.system.bean.TdmMessage;
import com.tjtt.tdm.system.bean.Users;
import com.tjtt.tdm.system.service.IMessageService;
import com.tjtt.tdm.system.service.LogService;

/**
 * @author Administrator
 *
 */
public class MessageAction extends BaseAction{
	private IMessageService messageService;
	private String tmNotice;
	private String tmNoticeType;
	private String tmDepts;
	private String tmUsers;
	private String tmUsersid;
	private String tmNoticeTitle;
	private LogService logService;
	
	public IMessageService getMessageService() {
		return messageService;
	}

	public void setMessageService(IMessageService messageService) {
		this.messageService = messageService;
	}
	 

	/**
	 * 查询消息
	 *  void
	 * @author 李数
	 * @date 2012-9-27 下午08:49:15
	 */
	public void queryGonggao(){
		try {
			//取出页面中的查询条件
			Map<String, String> filterMap = getPagerMap();
		String messageName ;
		messageName = getRequest().getParameter("messageName");
		if(messageName != null && !"".equals(messageName)){
			messageName = StringUtil.encodingUrl(messageName);
			messageName =StringUtil.escapeString(messageName);
		}
		String messageType ;
		messageType = getRequest().getParameter("messageType");
		if(messageName != null && !"".equals(messageType)){
			messageType = StringUtil.encodingUrl(messageType);
			messageType =StringUtil.escapeString(messageType);
		}
		
			//将查询条件保存到Map里
		filterMap.put("messageName", messageName);
		filterMap.put("messageType", messageType);
			String[] viewArray = { "id", "tm_user_id", "tm_notice_title",
					"username","tm_input_time", "tm_status:[{'0':'失效','1':'正常'}]", "tm_notice_type:[{'0':'系统公告','1':'个人消息'}]"};
			// 查询所有的数据
			PageResult result = messageService.queryGonggao(filterMap);
			
			String returnStr = getColumnJson(result, viewArray);
			setContentType();
			PrintWriter out = getResponse().getWriter();
			System.out.println(returnStr);
			out.print(returnStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//首页
	public String viewShouyeExec() throws Exception{
		//站内消息
		Users user = this.getUser();
		Map<String, String> mfilterMap = getPagerMap();
		mfilterMap.put("userid", user.getUserid().toString());
		mfilterMap.put("types", "1");
		List messagelist = messageService.findMessage(mfilterMap);
		List znList = new ArrayList();
		Object[] obj = null;
		for (int i = 0; i < messagelist.size(); i++) {
			obj = (Object[])messagelist.get(i);
			znList.add(obj);
		}
		
		Map<String, String> xfilterMap = getPagerMap();
		xfilterMap.put("types", "0");
		//系统公告
		List gonggao = messageService.findGonggao(xfilterMap);
		List ggList = new ArrayList();
		Object[] obj1 = null;
		for (int j = 0; j < gonggao.size(); j++) {
			obj1 = (Object[])gonggao.get(j);
			ggList.add(obj1);
		}
		
		String role = (String)getSession().getAttribute("ROLE_INFO");
		Map<String, String> dfilterMap = getPagerMap();
		dfilterMap.put("userId", getUserId());
		dfilterMap.put("status", "0");
		dfilterMap.put("role", role);
		//待办事项
		List waitlist = messageService.findDaiBanList(dfilterMap);
		List dbList = new ArrayList();
		Object[] obj2 = null;
		for (int t = 0; t < waitlist.size(); t++) {
			obj2 = (Object[])waitlist.get(t);
			dbList.add(obj2);
		}
		getRequest().setAttribute("messagelist", znList);
		getRequest().setAttribute("gonggao", ggList);
		getRequest().setAttribute("waitlist", dbList);
		return SUCCESS;
	}
	
	public void queryMessage(){
		try {
			Map<String, String> filterMap = getPagerMap();
			String type = getRequest().getParameter("type");
			String isreceive = getRequest().getParameter("isreceive");
			String isread = getRequest().getParameter("isread");
			String messageName = getRequest().getParameter("messageName");
		    //取出页面中的查询条件
			filterMap.put("tm_notice_type", type);
			filterMap.put("tm_user_id", String.valueOf(getUserId()));
			filterMap.put("isreceive", isreceive);
			filterMap.put("isread", isread);
			filterMap.put("messageName", messageName);
			//将查询条件保存到Map里
			// 查询所有的数据
			String returnStr = null;
			PageResult result = null;
			if("0".equals(type)){
				String[] viewArray = { "id", "tm_user_id", "tm_notice_title",
						"username","tm_input_time", "tm_status:[{'0':'失效','1':'正常'}]", 
						"tm_notice_type:[{'0':'系统公告','1':'个人消息'}]"};
				result = messageService.queryGonggao(filterMap);
				 returnStr = getColumnJson(result, viewArray);
			}else if("1".equals(type)){
				String[] viewArray = { "id", "tm_notice_title",
						"username","tm_input_time", "tm_status:[{'0':'失效','1':'正常'}]", 
						"tm_notice_type:[{'0':'系统公告','1':'个人消息'}]","ttm_state:[{'0':'未读','1':'已读'}]"};
				result = messageService.queryMessage(filterMap);
				 returnStr = getColumnJson(result, viewArray);
			}
			
			setContentType();
			PrintWriter out = getResponse().getWriter();
			System.out.println(returnStr);
			out.print(returnStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 添加消息
	 * @return String
	 * @author 李数
	 * @throws ServiceException 
	 * @date 2012-9-27 下午08:47:08
	 */
	public String addMessage() throws ServiceException{
		try {
			String tmNoticeType = getRequest().getParameter("tmNoticeType");
			TdmMessage message = new TdmMessage();
			BeanUtils.copyProperties(message, this);
			if(tmNoticeType != null && "0".equals(tmNoticeType)){
				message.setTmUsers(null);
				message.setTmUsersid(null);
			}
			message.setTmInputTime(new Timestamp(System.currentTimeMillis()));
			message.setTmStatus(1l);
			message.setTmUserId(Long.parseLong(getUserId()));
			messageService.saveMessage(message);
			getRequest().setAttribute("msg", "保存成功！");
			
			logService.saveLogInfo(this.getLogBase(), "系统管理", "消息管理", getUserName()+" 消息增加成功", 0);
			
			return "add";
		} catch (Exception e) {
			e.printStackTrace();
			getRequest().setAttribute("msg", "保存失败！");
			logService.saveLogInfo(this.getLogBase(), "系统管理", "消息管理", getUserName()+" 消息增加失败", 0);
			return "error";
		}
	}
	/**
	 * 修改消息
	 * @return String
	 * @author 李数
	 * @throws ServiceException 
	 * @date 2012-9-27 下午08:48:00
	 */
	public String updateMessage() throws ServiceException{
		try {
			String tmNoticeType = getRequest().getParameter("tmNoticeType");
			String id = getRequest().getParameter("id");
			TdmMessage message = messageService.getOneMessage(id);
			BeanUtils.copyProperties(message, this);
			messageService.updateMessage(message);
			getRequest().setAttribute("msg", "修改成功！");
			logService.saveLogInfo(this.getLogBase(), "系统管理", "消息管理", getUserName()+" 消息修改成功", 0);
			return "gotoupdate";
		} catch (Exception e) {
			e.printStackTrace();
			getRequest().setAttribute("msg", "修改失败！");
			logService.saveLogInfo(this.getLogBase(), "系统管理", "消息管理", getUserName()+" 消息修改成功", 0);
			return "error";
		}
	}
 
	/**
	 * 跳转到修改消息页面
	 * @return String
	 * @author 李数
	 * @date 2012-9-27 下午08:48:07
	 */
	public String gotoUpdate(){
		try {
			String id = getRequest().getParameter("id");
			TdmMessage message = messageService.getOneMessage(id);
			getRequest().setAttribute("message", message);
			return "gotoupdate";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
	/**
	 * 查看消息
	 * @return String
	 * @author 李数
	 * @date 2012-9-28 上午09:18:02
	 */
	public String viewMessage(){
		try {
			String id = getRequest().getParameter("id");
			TdmMessage message = messageService.getOneMessage(id);
			getRequest().setAttribute("message", message);
			return "view";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
	/**
	 * 工装定点提醒
	 * @return String
	 */
	public String viewMyTool(){
		try {
			String id = getRequest().getParameter("id");
			TdmMessage message = messageService.getOneMessage(id);
			getRequest().setAttribute("message", message);
			return "view";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
	
	public void updateMessageUser()throws Exception{
		PrintWriter out = getResponse().getWriter();
		try {
			String msgid = getRequest().getParameter("msgid");
			messageService.updateMessageUser(String.valueOf(getUserId()), msgid);
			logService.saveLogInfo(this.getLogBase(), "系统管理", "消息管理", getUserName()+" 消息修改成功", 0);
			 out.print("{\"ajaxResult\":\"success\"}");
		} catch (Exception e) {
			e.printStackTrace();
			logService.saveLogInfo(this.getLogBase(), "系统管理", "消息管理", getUserName()+" 消息修改成功", 0);
			 out.print("{\"ajaxResult\":\"error\"}");
		}
	}
	/****
	 * 删除消息
	 *  void
	 * @author 李数
	 * @date 2012-9-27 下午08:48:48
	 */
	public void deleteMessage() throws Exception{
		PrintWriter out = getResponse().getWriter();
    	try {
			 String ids=getRequest().getParameter("ids");
			 if(ids !=null&&! ids.isEmpty()){
				 messageService.deleteMessage(ids);
			 }
			 logService.saveLogInfo(this.getLogBase(), "系统管理", "消息管理", getUserName()+" 消息删除成功", 0);
			   out.print("{\"ajaxResult\":\"success\"}");
		} catch (Exception e) {
			   e.printStackTrace();
			   logService.saveLogInfo(this.getLogBase(), "系统管理", "消息管理", getUserName()+" 消息删除成功", 0);
			   out.print("{\"ajaxResult\":\"error\"}");
		}
	}
 
	/**
	 * 获取设备到期数量
	 */
	public void getSheBeiCount(){
		PrintWriter out = PubMethod.GetPrintWriter(); 
		String orgId = getOrgIds();
		try {
			int count = messageService.getSheBeiCount(this.getDeptid(),this.getUserId());
			getRequest().setAttribute("msg", "读取成功！");
			String output = "{\"ajaxResult\":\"success\",\"count\":\""+count+"\"}";
			out.print(output);
		} catch (Exception e) {
			e.printStackTrace();
			getRequest().setAttribute("msg", "读取失败！");
			String output = "{\"ajaxResult\":\"error\"}";
			out.print(output);
		}
	}
	
	/**
	 * 获取证书过期用户
	 */
	public void getCerifyName(){
		PrintWriter out = PubMethod.GetPrintWriter(); 
		String orgId = getOrgIds();
		try {
			List list = messageService.getCerifyName();
			String userName = "";
			if(null !=list && list.size() > 0){
				for (int i = 0; i < list.size(); i++) {
					userName += list.get(i).toString()+",";
				}
			}else{
				userName = "无";
			}
			String output = "{\"ajaxResult\":\"success\",\"count\":\""+userName+"\"}";
			out.print(output);
		} catch (Exception e) {
			e.printStackTrace();
			String output = "{\"ajaxResult\":\"error\"}";
			out.print(output);
		}
	}
	
	public String getTmNotice() {
		return tmNotice;
	}

	public void setTmNotice(String tmNotice) {
		this.tmNotice = tmNotice;
	}

	public String getTmNoticeType() {
		return tmNoticeType;
	}

	public void setTmNoticeType(String tmNoticeType) {
		this.tmNoticeType = tmNoticeType;
	}

	public String getTmDepts() {
		return tmDepts;
	}

	public void setTmDepts(String tmDepts) {
		this.tmDepts = tmDepts;
	}

	public String getTmUsers() {
		return tmUsers;
	}

	public void setTmUsers(String tmUsers) {
		this.tmUsers = tmUsers;
	}

	public String getTmNoticeTitle() {
		return tmNoticeTitle;
	}

	public void setTmNoticeTitle(String tmNoticeTitle) {
		this.tmNoticeTitle = tmNoticeTitle;
	}

	public String getTmUsersid() {
		return tmUsersid;
	}

	public void setTmUsersid(String tmUsersid) {
		this.tmUsersid = tmUsersid;
	}

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}
	
	
}
